package com.google.code.googlefight;

import java.text.NumberFormat;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class AnimatedFightView extends SurfaceView implements
		SurfaceHolder.Callback {

	private Paint paint = new Paint();
	private NumberFormat format=NumberFormat.getIntegerInstance();
	
	private AnimationThread thread;

	private String[] words;
	private Long[] results;
	
	private boolean animRunning=false;
	private int animProgress = 0;
	private final int animProgressStart = 0;
	private final int animProgressTotal = 100;
	
	private static final int BAR_PADDING=10;
	
	private int backgroundColor=Color.WHITE;

	public AnimatedFightView(Context ctx, AttributeSet attrSet) {
		super(ctx, attrSet);
		
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		
		//init paint
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setTypeface(Typeface.SANS_SERIF);
		paint.setTextSize(20);
		paint.setAntiAlias(true);
	}
	
	public void setBackgroundColor(int color) {
		this.backgroundColor=color;
	}
	
	public void startAnimationThread() {
		if(thread!=null) {
			thread.stopAnimation();
		}
		
		thread=new AnimationThread(this);
		thread.start();
	}
	
	public void stopAnimationThread() {
		if (thread != null) {
			thread.stopAnimation();
			thread=null;
		}
	}

	public AnimatedFightView(Context context) {
		super(context);
	}

	public void animateResults(String[] words, Long[] results) {
		this.words=words;
		this.results = results;

		this.animProgress = animProgressStart;
		this.animRunning=true;
	}

	public void paint(Canvas canvas) {
		canvas.drawColor(backgroundColor);

		if (results != null) {
			long size1 = results[0];
			long size2 = results[1];

			long divide = Math.max(size1, size2) / canvas.getHeight();

			size1 /= divide;
			size2 /= divide;
			
			if(size1>canvas.getHeight()*7/8) size1=canvas.getHeight()*7/8;
			if(size2>canvas.getHeight()*7/8) size2=canvas.getHeight()*7/8;
			if(size1<canvas.getHeight()/8) size1=canvas.getHeight()/8;
			if(size2<canvas.getHeight()/8) size2=canvas.getHeight()/8;

			double factor=(double)animProgress/animProgressTotal;
			
			paint.setColor(Color.LTGRAY);
			canvas.drawRect(BAR_PADDING, canvas.getHeight() - (int)(factor*size1), canvas.getWidth() / 2-BAR_PADDING, getHeight(), paint);
			paint.setColor(Color.BLACK);
			canvas.drawText(words[0], canvas.getWidth()/4, canvas.getHeight()-(int)(factor*size1)-5, paint);

			paint.setColor(Color.LTGRAY);
			canvas.drawRect(canvas.getWidth()/2+BAR_PADDING, canvas.getHeight() - (int)(factor*size2), canvas.getWidth()-BAR_PADDING, canvas.getHeight(), paint);
			paint.setColor(Color.BLACK);
			canvas.drawText(words[1], canvas.getWidth()*3/4, canvas.getHeight()-(int)(factor*size2)-5, paint);
			
			if(animProgress>=animProgressTotal/2) {
				int alpha=((animProgress-animProgressTotal/2)*255/(animProgressTotal/2));
				paint.setARGB(alpha, 0, 0, 0);
				canvas.drawText(format.format(results[0]), canvas.getWidth()/4, canvas.getHeight()-5, paint);
				canvas.drawText(format.format(results[1]), canvas.getWidth()*3/4, canvas.getHeight()-5, paint);
			}
		}
	}

	public void onDraw(Canvas canvas) {
		paint(canvas);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		System.out.println("onChanged");
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		startAnimationThread();
		
		System.out.println("onCreated");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		System.out.println("onDestroyed");
		
		stopAnimationThread();
	}

	private class AnimationThread extends Thread {
		private boolean running = true;
		private Canvas canvas;
		private SurfaceHolder holder;
		private AnimatedFightView view;
		
		private long lastTimestamp=0;

		public AnimationThread(AnimatedFightView view) {
			this.view = view;
			this.holder = view.getHolder();
		}

		public void stopAnimation() {
			running = false;
		}

		public void run() {
			lastTimestamp=System.currentTimeMillis();
			long currentTime=0;
			
			while (running) {
				canvas = null;

				try {
					canvas = holder.lockCanvas(null);

					if (canvas != null) {

						view.paint(canvas);
					}

				} finally {

					if (canvas != null) {
						holder.unlockCanvasAndPost(canvas);
					}

				}

				currentTime=System.currentTimeMillis();
				if(animRunning) animProgress+=(currentTime-lastTimestamp)/15;
				//System.out.println(currentTime-lastTimestamp);
				if(animProgress>=animProgressTotal) {
					animRunning=false;
					animProgress=animProgressTotal;
				}
				lastTimestamp=currentTime;

				try {
					Thread.sleep(10);
				} catch (Exception e) {
				}
			}
		}
	}
}